#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int N = 2e5+5;

int n,m,K,aa[N],c[N],a[N],d[20][N];
int spe_pw;

int Run(int len){
    if (len==1) return 1;
    vector<int> v;
    for (int i=1; i<=len; i++) v.push_back(i);
    int k=0;
    while ((1<<k)<len) k++;
    for (int i=1; i<=k; i++){
        vector<int> nv;
        for (int j=0; j<v.size(); j+=2){
            int w=d[i][(j+2)/2];
            if (a[v[j+w]]>=i) nv.push_back(v[j+w]);
            else nv.push_back(v[j+1-w]);
        }
        v=nv;
    }
    assert(v.size()==1);
    return v[0];
}

ll ans[N];
void spe(){
    map<int,int> mp;
    for (int i=1; i<=m; i++){
        if (mp.count(c[i])){
            ans[i]=mp[c[i]];
        }
        else{
            ans[i]=mp[c[i]]=Run(c[i]);
        }
    }
    ll res=0;
    for (int i=1; i<=m; i++){
        res^=1ll*i*ans[i];
    }
    cout<<res<<"\n";
}

int vis[N],ra[N];
void solve(){
    if (spe_pw){
        spe();
        return;
    }
    for (int i=1; i<=n; i++) ra[i]=a[i];
    for (int i=1; i<=m; i++){
        ans[i]=0;
        int k=0;
        while (1<<(k)<c[i]) k++;
        for (int j=1; j<=(1<<K); j++) vis[j]=0;
        for (int j=c[i]+1; j<=(1<<k); j++) a[i]=0;
        for (int j=c[i]+1; j<=(1<<k); j++){
            a[j]=100;
            vis[Run((1<<(k)))]=1;
           // cout<<Run(1<<k)<<"!";
            a[j]=0;
        }
        vis[Run(1<<k)]=1;
        for (int j=1; j<=(1<<K); j++){
            if (vis[j]) ans[i]+=j;//,cout<<j<<":";
        }//cout<<"\n";
        for (int j=1; j<=n; j++){
            a[i]=ra[i];
        }
    }
    ll res=0;
    for (int i=1; i<=m; i++){
       // cout<<ans[i]<<" ";
        res^=1ll*i*ans[i];
    }
    cout<<res<<"\n";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    cin>>n>>m;
    for (int i=1; i<=n; i++){
        cin>>aa[i];
    }
    for (int i=1; i<=m; i++){
        cin>>c[i];
        spe_pw&=((c[i]&-c[i])==c[i]);
    }
    while ((1<<K)<n) K++;
    for (int i=1; i<=K; i++){
        for (int j=1; j<=(1<<(K-i)); j++){
            char ch;
            cin>>ch;
            d[i][j]=ch-'0';
        }
    }
    int t;
    cin>>t;
    while (t--){
        int x[4];
        for (int i=0; i<4; i++) cin>>x[i];
        for (int i=1; i<=n; i++){
            a[i]=aa[i]^(x[i%4]);
           // cout<<a[i]<<",";
        }
        //cout<<"\n";
        solve();
    }
    return 0;
}

/*
Things to check:
    - int/long long overflow?
    - mod, +mod, %mod?
    - array bounds?
    - Places where you think can't be wrong.
    - Functions should return if not "void".
    - what is the memory limit? the mod? the name etc.
Things to think of:
    - Be brave to guess and be optimistic.
    - Start from a trivial case.
    - What is the expected time to finish this problem?
Do not be afraid of a problem, try.
Do not devote all your time into a problem, look at other problems.
*/